/**
 * 
 */
package de.oszimtcc.timetableview;

import de.oszimtcc.framework.Logger;
import android.app.Activity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;


/**
 * @author reuter
 *
 */
public class ActivitySwipeDetector implements View.OnTouchListener
{
	private Activity activity;
	private BlockListView parent;
	static final int MIN_DISTANCE = 100;
	private float downX, downY, upX, upY;

	public ActivitySwipeDetector(BlockListView activity)
	{
	    this.parent = activity;
	}

	public void onRightToLeftSwipe()
	{
		Logger.Debug("RightToLeftSwipe!");
	}

	public void onLeftToRightSwipe()
	{
		Logger.Debug("LeftToRightSwipe!");
	}

	public void onTopToBottomSwipe()
	{
		Logger.Debug("onTopToBottomSwipe!");
	}

	public void onBottomToTopSwipe()
	{
		Logger.Debug("onBottomToTopSwipe!");
	}
	
	public void onClick(View v)
	{
		Logger.Debug("onClick!");
	}

	public boolean onTouch(View v, MotionEvent event) 
	{
	    switch(event.getAction())
	    {
	        case MotionEvent.ACTION_DOWN: 
	        {
	            downX = event.getX();
	            downY = event.getY();
	            return true;
	        }
	        case MotionEvent.ACTION_UP: 
	        {
	            upX = event.getX();
	            upY = event.getY();

	            float deltaX = downX - upX;
	            float deltaY = downY - upY;
	            
	            if(Math.abs(deltaX) < 10 && Math.abs(deltaY) < 10)
	            {
	            	this.onClick(v);
	            }

	            // swipe horizontal?
	            if(Math.abs(deltaX) > MIN_DISTANCE)
	            {
	                // left or right
	                if(deltaX < 0) { this.onLeftToRightSwipe(); return true; }
	                if(deltaX > 0) { this.onRightToLeftSwipe(); return true; }
	            }
	            else 
	            {
	            		Logger.Debug("Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
	                    return false; // We don't consume the event
	            }

	            // swipe vertical?
	            if(Math.abs(deltaY) > MIN_DISTANCE)
	            {
	                // top or down
	                if(deltaY < 0) { this.onTopToBottomSwipe(); return true; }
	                if(deltaY > 0) { this.onBottomToTopSwipe(); return true; }
	            }
	            else 
	            {
	            		Logger.Debug("Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
	                    return false; // We don't consume the event
	            }

	            return true;
	        }
	    }
	    return false;
	}
}
